# 跨平台的 libpqxx 查找
find_path(PQXX_INCLUDE_DIR
    NAMES pqxx/pqxx
    PATHS
        /usr/include
        /usr/local/include
        $ENV{PROGRAMFILES}/libpqxx/include
        C:/libpqxx/include
)

find_library(PQXX_LIBRARY
    NAMES 
        pqxx
        pqxx-static
        libpqxx
        libpqxx-static
    PATHS
        /usr/lib
        /usr/local/lib
        /usr/lib/x86_64-linux-gnu
        $ENV{PROGRAMFILES}/libpqxx/lib
        C:/libpqxx/lib
)

# 在 Windows 上查找调试版本
if(WIN32 AND MSVC)
    find_library(PQXX_LIBRARY_DEBUG
        NAMES 
            pqxxd
            pqxx-staticd
            libpqxxd
            libpqxx-staticd
        PATHS
            $ENV{PROGRAMFILES}/libpqxx/lib
            C:/libpqxx/lib
    )
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PQXX DEFAULT_MSG
    PQXX_LIBRARY PQXX_INCLUDE_DIR
)

if(PQXX_FOUND)
    set(PQXX_LIBRARIES ${PQXX_LIBRARY})
    set(PQXX_INCLUDE_DIRS ${PQXX_INCLUDE_DIR})
    
    if(NOT TARGET PQXX::pqxx)
        add_library(PQXX::pqxx UNKNOWN IMPORTED)
        set_target_properties(PQXX::pqxx PROPERTIES
            INTERFACE_INCLUDE_DIRECTORIES "${PQXX_INCLUDE_DIR}"
        )
        
        if(PQXX_LIBRARY_DEBUG AND PQXX_LIBRARY)
            # 区分调试和发布版本
            set_target_properties(PQXX::pqxx PROPERTIES
                IMPORTED_LOCATION "${PQXX_LIBRARY}"
                IMPORTED_LOCATION_DEBUG "${PQXX_LIBRARY_DEBUG}"
            )
        else()
            set_target_properties(PQXX::pqxx PROPERTIES
                IMPORTED_LOCATION "${PQXX_LIBRARY}"
            )
        endif()
    endif()
endif()

mark_as_advanced(PQXX_INCLUDE_DIR PQXX_LIBRARY PQXX_LIBRARY_DEBUG)